<?php
/* Redaxo language/domains addon by Paul Wehle - http://www.pcwehle.de */

if (!isset($page)) $page='';
$mypage = "rex_lang";        // only for this file

/* well, this addon is prepared to interact with the xtcommerce addon... */
$xtc = OOAddon :: isAvailable('xtcommerce') ? true : false;
	

if ($REX['REDAXO']) { 
	$addonPath = '';
	$addonPath = $REX['INCLUDE_PATH'].'/addons/'.$mypage;

	$REX['ADDON']['rxid'][$mypage] = "393";
	$REX['ADDON']['page'][$mypage] = $mypage; 
	$REX['ADDON']['perm'][$mypage] = "rex_lang[]";

	// add permissions to user administration
	$REX['PERM'][] = $REX['ADDON']['perm'][$mypage];
	$REX['ADDON']['version'][$mypage] = '1.0';
	$REX['ADDON']['author'][$mypage] = 'Paul Wehle';
	$REX['ADDON']['supportpage'][$mypage] = 'pcwehle.de';

}


$clangIsset = false;
$sql = new rex_sql();
$qry = "select d.language, d.domain, l.lang_icon, l.lang_icon_over, l.meta_language, l.xtcommerce_id from rex_393_domain d, rex_393_language l where d.is_main=1 and l.status=1 and l.id = d.language";

$sql->setQuery($qry);
$REX['LANGS'] = array();
	
/* we build an array with all main domains and the respective info */
for ($i=0;$i<$sql->getRows();$i++) {
	$REX['LANGS'][$sql->getValue("language")] = array();
	$REX['LANGS'][$sql->getValue("language")]['DOMAIN'] = $sql->getValue("domain");
	$REX['LANGS'][$sql->getValue("language")]['ICON'] = $sql->getValue("lang_icon");
	$REX['LANGS'][$sql->getValue("language")]['ICON_HOVER'] = $sql->getValue("lang_icon_over");
	$REX['LANGS'][$sql->getValue("language")]['META'] = $sql->getValue("meta_language");
	if ($xtc)
		$REX['LANGS'][$sql->getValue("language")]['XTCID'] = $sql->getValue("xtcommerce_id");
	if ($_SERVER['HTTP_HOST'] == $sql->getValue("domain") && !isset($_GET['clang']) && !$REX['REDAXO']) {
		/* seems we matched the current domain, so lets set current lang */
		$REX['CUR_CLANG'] = $sql->getValue("language");
		global $clang;
		$clang = $REX['CUR_CLANG'];
		$clangIsset = true;
	}			
	$sql->next();
}

if (!$REX['REDAXO']) {
	//print_r($REX['LANGS']);
	if (isset($_GET['sys'])) {
		$clangIsset = true; 
		/* used on Ajax and system file requests, ignore language settings */
	}
	
	if (isset($_GET['clang'])) {
		$clangIsset = true; 
	}
	
	if (!$clangIsset) {
		$sql->setQuery("select l.meta_language, md.domain from rex_393_domain d, rex_393_domain md, rex_393_language l where d.language = l.id and d.language = md.language and md.is_main = '1' and (d.domain = '" . $_SERVER['HTTP_HOST'] . "' or d.domain = 'www." . $_SERVER['HTTP_HOST'] . "')");
		header("HTTP/1.1 301 Moved Permanently");
		if ($sql->getRows()) {
			/* we got the domain in our DB and forward to the main domain
			for that language */
			header('Location: http://' . $sql->getValue("domain") . "" . $_SERVER['REQUEST_URI']);	
		} else {
			if(substr_count($_SERVER['HTTP_HOST'],".")>1) {
				$searchDomain = substr($_SERVER['HTTP_HOST'],strpos($_SERVER['HTTP_HOST'],".")+1);
			} else {
				$searchDomain = '%' . $_SERVER['HTTP_HOST'];
			}
			$qry = "select md.domain from rex_393_domain d, rex_393_domain md where md.is_main=1 and md.language = d.language and d.domain like '" . $searchDomain . "'";
			$sql->setQuery($qry);
			
			if ($sql->getRows())
				/* current domain is a subdomain of a known domain, we forward
				to the main domain of the respective language */
				/* this is still a bit ugly as that could turn up quite some 
				matches and takes the first randomly */
				header('Location: http://' . $sql->getValue("domain") . "" . $_SERVER['REQUEST_URI']);
			else {
				/* no idea what the current domain belongs to, so we forward
				to the main domain of the main language */
				header('Location: http://' . $REX['LANGS'][0]['DOMAIN'] . "" . $_SERVER['REQUEST_URI']);
			}
		}
	}
}
if ($REX['REDAXO']) { 
	$I18N_393 = new i18n($REX['LANG'], $addonPath.'/lang/'); 
	global $I18N_393;

	function a393_changeSpecialsNavi($params) {
		global $REX, $I18N, $I18N_393, $mypage;
	
		$content = $params['subject'];
	
		$search = '/<div id="rex-title">[\n\b\s\t]*<div class="rex-title-row"><h1>' . $I18N->msg('specials') . '<\/h1><\/div>[\n\b\s\t]*<div class="rex-title-row">[\n\b\s\t]*<ul>[\n\b\s\t]*<li><a href="\?page=specials&amp;subpage=" tabindex="[0-9]+" class="rex-subpage-active">' . $I18N->msg('main_preferences') . '<\/a> \| <\/li>[\n\b\s\t]*<li><a href="\?page=specials&amp;subpage=lang" tabindex="[0-9]+">' . $I18N->msg('languages') . '<\/a><\/li>[\n\b\s\t]*<\/ul>[\n\b\s\t]*<\/div>[\n\b\s\t]*<\/div>[\n\b\s\t]*<!-- \*\*\* OUTPUT OF CONTENT - START \*\*\* -->[\n\b\s\t]*<div id="rex-output">/';
		
		$replace = '<div id="rex-title">
  		<div class="rex-title-row"><h1>' . $I18N->msg('specials') . '</h1></div>
  		<div class="rex-title-row">
      <ul>
        <li><a href="?page=specials&amp;subpage=" tabindex="25" class="rex-subpage-active">' . $I18N->msg('main_preferences') . '</a> | </li>
        <li><a href="?page=rex_lang&amp;subpage=lang" tabindex="26">' . $I18N->msg('languages') . '</a> | </li>
		<li><a href="?page=rex_lang&amp;subpage=domain" tabindex="27">'.$I18N_393->msg("label_domains").'</a></li>
        
      </ul>
      </div>
	</div>
<!-- *** OUTPUT OF CONTENT - START *** -->
	<div id="rex-output">';
		
		
		$content = preg_replace($search,$replace,$content);
		
		return $content;	
	}
	
	function a393_changePreviewLink($params) {
		global $REX, $I18N, $I18N_393, $mypage, $article_id, $clang;

		$content = $params['subject'];
		//<a href="../index.php?article_id=1&amp;clang=1" onclick="window.open(this.href); return false;"  tabindex="35">Anzeigen</a>
		$search = '@<a href="\.\./index\.php\?article_id=' . $article_id . '&amp;clang=' . $clang . '" onclick="window\.open\(this\.href\); return false;"  tabindex="([0-9]+)">' . $I18N->msg('show') . '</a>@';
		$replace = '<a href="http://'.$REX['LANGS'][$clang]['DOMAIN'].'/index.php?article_id=' . $article_id . '&amp;clang=' . $clang . '" onclick="window.open(this.href); return false;" tabindex="$1">' . $I18N->msg('show') . '</a>';
		$content = preg_replace($search,$replace,$content);
		return $content;
	}
	
	if ($_REQUEST['page']=='specials') {
		rex_register_extension('OUTPUT_FILTER', 'a393_changeSpecialsNavi');
	}
	if ($_REQUEST['page']=='content' && ($_REQUEST['mode'] == 'edit' || $_REQUEST['mode'] == '')) {
		if ($REX['LANGS'][$clang]['DOMAIN'] != '')
			rex_register_extension('OUTPUT_FILTER', 'a393_changePreviewLink');
	}
}
?>